<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );

class EcommerceModelOrders extends JModel
{
	function __construct()
	{
		parent::__construct();
	}
	
	function getOrderDetail($order_id){
	//die('aaabbbb');
		$query = "SELECT * FROM #__pr_orders o LEFT JOIN #__users u ON o.order_user_id = u.id LEFT JOIN #__users_extension ue ON o.order_user_id = ue.id WHERE o.order_id  = ". $order_id ;
		$this->_db->setQuery($query);	
		//var_dump($query);
		if($order = $this->_db->loadObject()){
				//var_dump($order);
			$query = "SELECT c.*, p.number FROM #__pr_cart c LEFT JOIN #__pr_product p ON c.product_id = p.id WHERE c.order_id  = " . $order_id . "  ORDER BY p.id";
			$this->_db->setQuery( $query );
			$order->products = $this->_db->loadObjectList();
			//var_dump($query);
		}
		return $order;
		
	}
	
	function getList()
	{	
		$user = JFactory::getUser();
		
		$query = "SELECT * FROM #__pr_orders WHERE order_user_id  = " . $user->id . "  ORDER BY order_date DESC ";
		
		$this->_db->setQuery($query);	
		$orders = $this->_db->loadObjectList();
		
		/*$i = 0;
		foreach($orders as $order){
			$query = "SELECT pp.name, pt.name, pd.price 
					FROM (#__pr_cart AS cart LEFT JOIN #__pr_productdetail AS pd ON cart.productdetail_id = pd.id)  
					LEFT JOIN #__pr_product_category AS pc ON pd.product_category_id = pc.id 
					LEFT JOIN #__pr_producttype AS pt ON pt.id = pd.producttype_id 
					LEFT JOIN #__pr_product AS pp ON pc.product_id = pp.id 
					WHERE cart.order_id = ".$order->order_id;
			echo $query;
			$this->_db->setQuery($query);
			$orders[$i]->cart = $this->_db->loadObjectList();
			$i++;
		}*/

		return $orders;	
	}
	
	function getListCategories()
	{
		$query = "SELECT id, alias, name FROM #__pr_category WHERE `published`=1";
		$this->_db->setQuery($query);
		$list = $this->_db->loadObjectList();

		return $list;
	}
	
	function getProductByORdersID($orders_id, $category_id){
		$lim   = JRequest::getInt('limit',10);
		$lim0  = JRequest::getVar('limitstart', 0, '', 'int');
		
		
		$q = "SELECT SQL_CALC_FOUND_ROWS pp.name, pt.name as ptype, pd.price  
				FROM (((#__pr_cart AS cart LEFT JOIN #__pr_productdetail AS pd ON cart.productdetail_id = pd.id)
				LEFT JOIN #__pr_product_category AS pc ON pd.product_category_id = pc.id)
				LEFT JOIN #__pr_producttype AS pt ON pt.id = pd.producttype_id)
				LEFT JOIN #__pr_product AS pp ON pc.product_id = pp.id
				WHERE cart.order_id = ".$orders_id . " AND pc.category_id=".$category_id;
		
		$this->_db->setQuery($q);
		$rL= $this->_db->loadObjectList();
		if (empty($rL)) {
			return;
		}
		else {
			$this->_db->setQuery('SELECT FOUND_ROWS();');  //no reloading the query! Just asking for total without limit
			jimport('joomla.html.pagination');
			$pageNav = new JPagination( $this->_db->loadResult(), $lim0, $lim );
				
		}
		//$list = $this->_db->loadObjectList();
		$result["link"] = $pageNav->getPagesLinks(); //Displays a nice footer
		$result["data"] = $rL;
		return $result;
	}
	
}
?>